﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Security.Cryptography;
using System.Xml;
using WeChatLibrary.CheckSignUtility;

namespace WeChatLibrary.GetMsgServer
{
    public class WeChatServer
    {
        /// <summary>   
        /// 处理微信发来的请求    
        /// </summary>   
        /// <param name="xml"></param>   
        public string processRequest(String xml)
        {
            try
            {
                // xml请求解析     
                Hashtable requestHT = xml.ParseXmlToHashtable();

                // 发送方帐号（open_id）     
                string fromUserName = (string)requestHT["FromUserName"];
                // 公众帐号     
                string toUserName = (string)requestHT["ToUserName"];
                // 消息类型     
                string msgType = (string)requestHT["MsgType"];

                //文字消息   
                if (msgType == WeChatReqMsgType.Text)
                {
                    string content = (string)requestHT["Content"];
                    if (content == "1")
                    {
                        return content;//GetNewsMessage(toUserName, fromUserName);
                    }

                    // Response.Write(GetMainMenuMessage(toUserName, fromUserName, "你好，我是你爹xwd,"));
                    return "";
                }
                else if (msgType == WeChatReqMsgType.Event)
                {
                    // 事件类型     
                    String eventType = (string)requestHT["Event"];
                    // 订阅     
                    if (eventType == ReqEventType.Subscribe.ToString())
                    {

                        //  Response.Write(GetMainMenuMessage(toUserName, fromUserName, "谢谢您的关注！,"));

                    }
                    // 取消订阅     
                    else if (eventType == ReqEventType.Unsubscribe.ToString())
                    {
                        // TODO 取消订阅后用户再收不到公众号发送的消息，因此不需要回复消息     
                    }
                    // 自定义菜单点击事件     
                    else if (eventType == ReqEventType.CLICK.ToString())
                    {
                        // TODO 自定义菜单权没有开放，暂不处理该类消息     
                    }
                    return eventType;
                }
                else if (msgType == WeChatReqMsgType.Location)
                {
                    return msgType;
                }

                return "";
            }
            catch (Exception e)
            {
                return e.Message.ToString();
            }
        }

        public string Message_Text
        {
            get
            {
                return @"<xml>
                            <ToUserName><![CDATA[{0}]]></ToUserName>
                            <FromUserName><![CDATA[{1}]]></FromUserName>
                            <CreateTime>{2}</CreateTime>
                            <MsgType><![CDATA[text]]></MsgType>
                            <Content><![CDATA[{3}]]></Content>
                            </xml>";
            }
        }
    }
}
